'use strict';

const extend = require('./app/core/model')

module.exports = app => {
  app.beforeStart(async function () {

    //处理未处理的异常 跳出异步调用的代码，错误很难捕获。
    process.on('uncaughtException', function (err) {
      console.log('Caught exception: ', err);
      app.logger.error('监听到未处理的异常：', err);
    });

    // 同步数据库表
    // 建立模型之间的关系
    /**
      hasOne - 与目标模型建立1:1关联关系，关联关系(外键)存在于目标模型中。详见：Model.hasOne()
      belongsTo - 与目标模型建立1:1关联关系，关联关系(外键)存在于源模型中。详见：Model.belongsTo()
      hasMany - 与目标模型建立1:N关联关系，关联关系(外键)存在于目标模型中。详见：Model.hasMany()
      belongsToMany - 与目标模型建立N:M关联关系，会通过sourceId和targetId创建交叉表。详见：Model.belongsToMany()
     */
    // 报修表和明细表  1:n
    app.model.Repair.hasMany(app.model.RepairSign, { foreignKey: 'repair_id', constraints: false });
    app.model.RepairSign.belongsTo(app.model.Repair, { foreignKey: 'repair_id', targetKey: 'id', constraints: false });

    // 报修表和报修工作流表
    app.model.Repair.hasMany(app.model.RepairWorkflow, { foreignKey: 'workflow_id', targetKey: 'id', constraints: false });
    app.model.RepairWorkflow.belongsTo(app.model.Repair, { foreignKey: 'workflow_id', targetKey: 'id', constraints: false });

    //客户表和联系人表 1:n
    app.model.Customer.hasMany(app.model.Contact, { foreignKey: 'customer_id', constraints: false })
    app.model.Contact.belongsTo(app.model.Customer, { foreignKey: 'customer_id', constraints: false })
    //客户表和用户表
    app.model.User.hasMany(app.model.Customer, { foreignKey: 'created_userid', targetKey: 'userid'  , constraints: false })
    app.model.Customer.belongsTo(app.model.User, { foreignKey: 'created_userid', targetKey: 'userid'  , constraints: false })
    //客户和批次表 1:n
    app.model.Customer.hasMany(app.model.Batch, { foreignKey: 'customer_id', constraints: false })
    app.model.Batch.belongsTo(app.model.Customer, { foreignKey: 'customer_id', constraints: false })
    //批次和批次产品表 1:n
    app.model.Batch.hasMany(app.model.BatchProduct, { foreignKey: 'batch_id', constraints: false })
    //客户表和拜访记录表 1:n
    app.model.Customer.hasMany(app.model.Visit, { foreignKey: 'customer_id', constraints: false })
    app.model.Visit.belongsTo(app.model.Customer, { foreignKey: 'customer_id', constraints: false })
    //联系人和拜访记录表 1:n
    app.model.Contact.hasMany(app.model.Visit, { foreignKey: 'contact_id', constraints: false })
    app.model.Visit.belongsTo(app.model.Contact, { foreignKey: 'contact_id', constraints: false })
    //用户表和拜访记录表 1:n
    app.model.User.hasMany(app.model.Visit, { foreignKey: 'userid', constraints: false })
    app.model.Visit.belongsTo(app.model.User, { foreignKey: 'userid', constraints: false })
    //拜访记录表和拜访费用表 1:n
    app.model.Visit.hasMany(app.model.VisitFee, { foreignKey: 'visit_id', constraints: false })
    app.model.VisitFee.belongsTo(app.model.Visit, { foreignKey: 'visit_id', constraints: false })


    /* 项目相关表 */
    //项目表和项目产品表 1:n
    app.model.Project.hasMany(app.model.ProjectProduct, { foreignKey: 'proj_id', constraints: false, as: 'products' })
    app.model.ProjectProduct.belongsTo(app.model.Project, { foreignKey: 'proj_id', constraints: false })
    //项目表和项目成本表 1:n
    app.model.Project.hasMany(app.model.ProjectCost, { foreignKey: 'proj_id', constraints: false, as: 'costs' })
    app.model.ProjectCost.belongsTo(app.model.Project, { foreignKey: 'proj_id', constraints: false })

    //角色表和用户表 1:n 
    app.model.Role.hasMany(app.model.User, { foreignKey: 'role_id', targetKey: 'id', constraints: false })
    app.model.User.belongsTo(app.model.Role, { foreignKey: 'role_id', targetKey: 'id', constraints: false })
    //角色表和权限表 1:n
    app.model.Role.hasMany(app.model.RolePower, { foreignKey: 'role_id', targetKey: 'id', constraints: false })
    app.model.RolePower.belongsTo(app.model.Role, { foreignKey: 'role_id', targetKey: 'id', constraints: false })
    //用户表和部门表
    app.model.Dept.hasMany(app.model.User, { foreignKey: 'department', targetKey: 'id', constraints: false })
    app.model.User.belongsTo(app.model.Dept, { foreignKey: 'department', targetKey: 'id', constraints: false })

    // force:true  删表重建，仅在开发阶段启用
    // await app.model.sync({ force: true });
    await app.model.sync({ alter: true });

    //给所有模型增加拓展方法
    for (var item in app.model) {
      if (app.model[item].tableName) {
        Object.assign(app.model[item], extend)
      }
    }
  });

};

